package com.gss.module.generator.core;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.gss.common.utils.StringUtils;
import com.gss.module.generator.constants.CommonConstants;
import com.gss.module.generator.entity.BaseEntity;
import com.gss.module.generator.entity.FieldEntity;

import java.util.Date;
import java.util.List;

/**
 * @author DYang
 * @date 2023/5/25
 * 实体类生成器
 */
public class EntityGenerator {

    private static  String ENTITY_PATH = "classpath:template/EntityGeneratorTemplate.template";
    private static  String FIELD_PATH = "classpath:template/FieldGeneratorTemplate.template";

    /**
     * 根据内如构建实体类
     * @param fileDir
     * @param baseEntity
     */
    public static boolean builder(String fileDir,BaseEntity baseEntity){
        //构建实体类
        String content = buildEntityInfo(baseEntity,ENTITY_PATH);
        if (content == null){
            System.out.println("模板不存在，暂未解析到数据");
            return false;
        }
        List<FieldEntity> fieldList = baseEntity.getFieldList();
        //构建字段
        String fieldsInfo = buildFieldInfo(fieldList,FIELD_PATH);
        if (fieldsInfo == null){
            fieldsInfo = "";
        }
        content = content.replace("${CLASS_FIELDS_INFO}",fieldsInfo);

        //存储到指定指定文件
        saveEntityFile(baseEntity.getClassName(),content,fileDir);
        return true;
    }

    /**
     * 将数据写道指定文件
     * @param className
     * @param content
     * @param fileDir
     */
    private static void saveEntityFile(String className,String content,String fileDir) {

        fileDir = fileDir + "/" + className + CommonConstants.JAVA_FILE;
        FileUtil.writeString(content, fileDir, "UTF-8");
    }

    /**
     * 构建实体类
     * @param baseEntity
     * @param entityTemplatePath
     * @return
     */
    private static String buildEntityInfo(BaseEntity baseEntity, String entityTemplatePath) {
        String content = ResourceUtil.readUtf8Str(entityTemplatePath);
        if (StringUtils.isEmpty(content)) {
            return null;
        }
        content = content.replace("${AUTHOR}",baseEntity.getAuthor());
        content = content.replace("${DATE}", DateUtil.format(new Date(),"yyyy/MM/dd"));
        content = content.replace("${ENTITY_NAME}",baseEntity.getEntityName());
        content = content.replace("${ENTITY_DESC}",baseEntity.getEntityDesc());
        content = content.replace("${TABLE_NAME}",baseEntity.getTableName());
        content = content.replace("${SCOPE}",baseEntity.getScope());
        content = content.replace("${CLASS_NAME}",baseEntity.getClassName());
        return content;
    }

    /**
     * 构建字段实体类
     * @param fieldList
     * @param fieldTemplatePath
     * @return
     */
    private static String buildFieldInfo(List<FieldEntity> fieldList, String fieldTemplatePath) {
        String template = ResourceUtil.readUtf8Str(fieldTemplatePath);
        if (CollUtil.isEmpty(fieldList) || StringUtils.isEmpty(template)) {
            return null;
        }
        StringBuffer buffer = new StringBuffer();

        for (FieldEntity entity : fieldList) {
            String content = template;
            content = content.replace("${FIELD_DESC}",entity.getFieldDesc());
            content = content.replace("${SCOPE}", entity.getScope());
            content = content.replace("${FIELD_TYPE}",entity.getFieldType());
            content = content.replace("${FIELD_NAME}",entity.getFieldName());

            buffer.append(content + "\n\n");
        }
        return buffer.toString();
    }


}
